import {
  uuid as _uuid,
  types as _types,
  showPrimaryKey as _show_primary_key,
  hasColumn as _has_column,
  addColumn as _add_column,
  showColumns as _show_columns,
  showTables as _show_tables,
  hasTable as _has_table,
  dropTable as _drop_table,
  truncateTable as _truncate_table,
  renameTable as _rename_table,
  createTable as _create_table,
  insertHandler as _insert_handler,
  updateHandler as _update_handler,
  deleteHandler as _delete_handler,
  updateByIdHandler as _update_by_id_handler,
  saveHandler as _save_handler,
  selectHandler as _select_handler,
  selectWhereHandler as _select_where_handler,
  pageHandler as _page_handler,
  dropColumn as _drop_column,
  renameColumn as _rename_column
} from './actions.js'

export class DBConfig {
  static async uuid(): Promise<any> {
    return await _uuid()
  }

  static async types(): Promise<any> {
    return await _types()
  }

  static async primary_key(): Promise<any> {
    return await _show_primary_key()
  }

  static async tables(): Promise<any> {
    return await _show_tables()
  }
}

export class Table {
  name: string

  constructor(name: string) {
    this.name = `tb_${name}`
  }

  withColumn(): Column {
    return new Column(this.name)
  }

  async columns(includePrimaryKey: boolean): Promise<any> {
    return await _show_columns(this.name, includePrimaryKey)
  }

  async has(): Promise<any> {
    return await _has_table(this.name)
  }

  async drop(): Promise<any> {
    return await _drop_table(this.name)
  }

  async truncate(): Promise<any> {
    return await _truncate_table(this.name)
  }

  async rename(toName: string): Promise<any> {
    return await _rename_table(this.name, toName)
  }

  async create(data: any): Promise<any> {
    return await _create_table(this.name, data)
  }

  async insert(data: any): Promise<any> {
    return await _insert_handler(this.name, data)
  }

  async update(where: any, data: any): Promise<any> {
    return await _update_handler(this.name, where, data)
  }

  async delete(where: any): Promise<any> {
    return await _delete_handler(this.name, where)
  }

  async updateById(data: any): Promise<any> {
    return await _update_by_id_handler(this.name, data)
  }

  async deleteById(id: any): Promise<any> {
    return await _update_by_id_handler(this.name, id) 
  }

  async save(data: any): Promise<any> {
    return await _save_handler(this.name, data)
  }

  async select(methods: any = {}): Promise<any> {
    return await _select_handler(this.name, methods)
  }

  async selectConditional(where: any = {}): Promise<any> {
    return await _select_where_handler(this.name, where)
  }

  async page(page: any): Promise<any> {
    return await _page_handler(this.name, page)
  }
}

class Column {
  name: string

  constructor(name: string) {
    this.name = name
  }

  async has(name: string): Promise<any> {
    return await _has_column(this.name, name)
  }

  async drop(column: any): Promise<any> {
    return await _drop_column(this.name, column) 
  }

  async add(column: any): Promise<any> {
    return await _add_column(this.name, column)
  }

  async rename(column: any): Promise<any> {
    return await _rename_column(this.name, column) 
  }
}
